package cn.iocoder.yudao.module.resource.controller.admin.audiocommand;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.resource.controller.admin.audiocommand.vo.*;
import cn.iocoder.yudao.module.resource.dal.dataobject.audiocommand.AudioCommandDO;
import cn.iocoder.yudao.module.resource.service.audiocommand.AudioCommandService;

@Tag(name = "管理后台 - 语音指令配置")
@RestController
@RequestMapping("/resource/audio-command")
@Validated
public class AudioCommandController {

    @Resource
    private AudioCommandService audioCommandService;

    @PostMapping("/create")
    @Operation(summary = "创建语音指令配置")
    @PreAuthorize("@ss.hasPermission('resource:audio-command:create')")
    public CommonResult<Long> createAudioCommand(@Valid @RequestBody AudioCommandSaveReqVO createReqVO) {
        return success(audioCommandService.createAudioCommand(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新语音指令配置")
    @PreAuthorize("@ss.hasPermission('resource:audio-command:update')")
    public CommonResult<Boolean> updateAudioCommand(@Valid @RequestBody AudioCommandSaveReqVO updateReqVO) {
        audioCommandService.updateAudioCommand(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除语音指令配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:audio-command:delete')")
    public CommonResult<Boolean> deleteAudioCommand(@RequestParam("id") Long id) {
        audioCommandService.deleteAudioCommand(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得语音指令配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:audio-command:query')")
    public CommonResult<AudioCommandRespVO> getAudioCommand(@RequestParam("id") Long id) {
        AudioCommandDO audioCommand = audioCommandService.getAudioCommand(id);
        return success(BeanUtils.toBean(audioCommand, AudioCommandRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得语音指令配置分页")
    @PreAuthorize("@ss.hasPermission('resource:audio-command:query')")
    public CommonResult<PageResult<AudioCommandRespVO>> getAudioCommandPage(@Valid AudioCommandPageReqVO pageReqVO) {
        PageResult<AudioCommandDO> pageResult = audioCommandService.getAudioCommandPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AudioCommandRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出语音指令配置 Excel")
    @PreAuthorize("@ss.hasPermission('resource:audio-command:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAudioCommandExcel(@Valid AudioCommandPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AudioCommandDO> list = audioCommandService.getAudioCommandPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "语音指令配置.xls", "数据", AudioCommandRespVO.class,
                        BeanUtils.toBean(list, AudioCommandRespVO.class));
    }

}